function err = avg_err(m, theta)

sum_m = sum(m, 3);
sum_m = repmat(sum_m, [1 1 size(m, 3)]);
nonzero_num = sum(theta ~= 0, "all");
err = abs(m ./ sum_m - theta);
err =  err / nonzero_num;
err(isnan(err)) = 0;
err(err == inf) = 0;
err = sum(err, "all");

end % function